package com.xqh.service.impl.ques;

import com.xqh.base.dao.CrudMapper;
import com.xqh.base.service.impl.BaseCrudServiceImpl;
import com.xqh.dao.ques.BookMapper;
import com.xqh.entity.ques.Book;
import com.xqh.service.ques.BookService;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 
 * @author leo
 * @date 2018-04-18 12:24:39
 * @version 1.0.0
 * @copyright www.vip-young.com
 */
@Service("bookService")
public class BookServiceImpl extends BaseCrudServiceImpl<Book> implements BookService {

    @Resource
    private BookMapper bookMapper;

    @Override
    public CrudMapper init() {
        return bookMapper;
    }

    @Override
    public List<Book> getBookListWithUnit(Map<String, Object> map) {
        List<Book> books = bookMapper.getBookListWithUnit(map);
        List<Book> resList = new ArrayList<>();
        Set<String> set = new HashSet<>();
        for(Book b:books){
            if(!set.contains(b.getCode())){
                set.add(b.getCode());
                resList.add(b);
            }
        }
        for(Book b:resList){
            b.setUnitList(books.stream().filter(bs-> bs.getCode().equals(b.getCode())).map(Book::getUnit).collect(Collectors.toList()));
        }

        return resList;
    }
}